package com.ht.right.action;



import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.ht.right.entity.Role;
import com.ht.right.entity.User;
import com.ht.right.entity.UserToRole;
import com.ht.right.service.OrgFacadeService;
import com.ht.right.util.CommonUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class OrgFacadeAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private OrgFacadeService orgFacadeService;
	private User user;
	private Role role;
	private UserToRole userToRole;
	
	
	
	//-----------------部门用户关联操作开始-------------------
	public String addUser(){
		String html =null;
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			String sameLevelNodeId = request.getParameter("sameLevelNodeId");
			orgFacadeService.addUser(user,sameLevelNodeId);
			html = "<script>parent.LoadUserInfo();parent.goCloseDlg(\"oWinDlg\");</script>";
		}catch(Exception e){
			html = "<script>alert('添加用户失败！');parent.goCloseDlg(\"oWinDlg\");</script>";
			e.printStackTrace();
		}
		CommonUtil.toString(ServletActionContext.getResponse(), html);
		return null;
		
	}
	
	public String login(){
		Map<String,Object> map =orgFacadeService.login(user);
		if(CommonUtil.isNullOrEmpty(map)){
			return "login";
		}else{
			ActionContext.getContext().getSession().putAll(map);
			return "success";
		}
	}
	

	
	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public UserToRole getUserToRole() {
		return userToRole;
	}

	public void setUserToRole(UserToRole userToRole) {
		this.userToRole = userToRole;
	}

	public String deleteDept(){
		String deptId = ServletActionContext.getRequest().getParameter("deptId");
		orgFacadeService.deleteDept(deptId);
		return null;
		
	}
	
	public String findOrgTree(){
		CommonUtil.toJsonStr( ServletActionContext.getResponse(), orgFacadeService.findOrgTree());
		return null;
		
	}
	
	//-----------------部门用户关联操作结束-------------------
	
	// -----------------角色用户关联操作开始-------------------
	
		public String saveUserToRole(){
			HttpServletRequest request = ServletActionContext.getRequest();
			String userList = request.getParameter("Members");
			orgFacadeService.saveUserToRole(role,userList);
			String html = "<script>parent.roleLoad();parent.CloseWindow();</script>";
			CommonUtil.toString(ServletActionContext.getResponse(), html);
			return null;
		}
		
		public String findUserIdByRole(){
			userToRole = new UserToRole();
			userToRole.setRole(role);
			CommonUtil.toJsonStr(ServletActionContext.getResponse(), orgFacadeService.findUserIdByRole(role));
			return null;
		}
		
		
		public String updateUserToRole(){
			HttpServletRequest request = ServletActionContext.getRequest();
			String addUserList = request.getParameter("Members");
			userToRole.setRole(role);
			orgFacadeService.updateUserToRole(role,userToRole,addUserList);
			String html = "<script>parent.roleLoad();parent.CloseWindow();</script>";
			CommonUtil.toString(ServletActionContext.getResponse(), html);
			return null;
		}
		
		// -----------------角色用户关联操结束-------------------
	

	public OrgFacadeService getOrgFacadeService() {
		return orgFacadeService;
	}

	public void setOrgFacadeService(OrgFacadeService orgFacadeService) {
		this.orgFacadeService = orgFacadeService;
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	
	
	
}
